<?php
class Episodes extends Podcast {

	public function getContent() {
		$content = '<a href="'.ROOT.'#'.self::$podcast.'">back</a><br><br>';
		$feed = new Feed();
		$player = new AudioPlayer();

		// get all items (all epsidoes) and display in a nice div
		$pattern = "#<item>(.*)</item>#Us";
		$feedContent = $feed->getCachedContent();
		if(preg_match_all($pattern, $feedContent, $matches)) {

			// basic podcast information (feeds, url etc.)
			$query = "SELECT name, feed, torrentfeed FROM `pp_podcasts` WHERE tag = '".self::$podcast."';";
			$result = self::$db->query($query);
				
			if(mysql_num_rows($result)) {
				list($podcastname, $feedURL, $torrentURL) = @mysql_fetch_array($result);
					
				preg_match("#<link>(.*)</link>#Us", $feedContent, $link);
				$link = @$link[1];
				
				$content .= '<table><tr><td style="vertical-align: top;"><h2>'.$podcastname.'</h2></td><td>&nbsp;</td>';
				$content .= '<td><a href="'.$link.'" title="Go to website">'.$link.'</a><br>';
				$content .= '<a href="'.$feedURL.'" title="Go to podcast feed">'.$feedURL.'</a><br>';
				$content .= '<a href="'.$torrentURL.'" title="Go to torrent feed">'.$torrentURL.'</a></td></tr></table>';
			} else {
				$podcastname = "deleted";
			}

			// show all episodes, which are already cached
			foreach ($matches[1] as $item) {
				// find the episode title, link and filename
				preg_match('#.*<title>(.*)[\r\n]?</title>#', $item, $match);
				$title = @$match[1];

				preg_match('#.*<link>(.*)[\r\n]?</link>#', $item, $match);
				$link = @$match[1];

				preg_match('#<enclosure.*url=".*/(.*\.[ampAMP34]{3})#', $item, $match);
				$filename = @$match[1];

				preg_match('#.*<pubDate>(.*)[\r\n]?</pubDate>#', $item, $match);
				$pub = @$match[1];
				($pub == "") ? $pub = "n/a" : $pub = date("d.m.Y", strtotime($pub));

				($link != "") ? $title = ' <a href="'.$link.'" target="_blank">'.$title.'</a>' : $link;

				$content .= '<article id="'.$filename.'" class="item">
							<div>
								<img src="'.ROOT.'logo/'.self::$podcast.'" class="pclogo" alt="Podcastcover" />
								<div class="right">
									<p class="published"><span class="today">'.$pub.'</span></p>
								</div>
								<div>
									<h3 class="title">'.$title.'</h3>
									<p class="feed">'.$filename.'
									<a href="'.ROOT.'feed/'.self::$podcast.'/'.$filename.'"><img src="'.ROOT.'app/images/link.png" style="border: 0;" alt="Download file"/></a><br>
									by <a href="'.ROOT.'#'.self::$podcast.'">'.$podcastname.'</a></p>
								</div>
							</div>
							<br />
							'.$player->getPlayer(self::$podcast,$filename).'
							</article><br />';
			}
		} else {
			$content .= "<i>There are no episodes available for this podcast.</i>";
		}

		return $content;
	}
}

?>
